package com.yufei.infoExtractor.util.sequence;

import java.net.InetAddress;


import java.security.SecureRandom;

import org.apache.log4j.Logger;


public class SequenceUUID
{
  private static SequenceUUID oInstance = null;

  private static String midValue = null;

  private static Logger log = Logger.getLogger(SequenceUUID.class);

  public static synchronized SequenceUUID getInstance()
  {
    if (oInstance == null) {
      oInstance = new SequenceUUID();
    }
    return oInstance;
  }

  public String getUUID()
  {
    long timeNow = System.currentTimeMillis();
    int timeLow = (int)timeNow & 0xFFFFFFFF;

    if (midValue == null) {
      try {
        InetAddress inet = InetAddress.getLocalHost();
        byte[] bytes = inet.getAddress();
        String hexInetAddress = hexFormat(getInt(bytes), 8);
        String thisHashCode = hexFormat(System.identityHashCode(this), 8);

        midValue = hexInetAddress + thisHashCode;
      }
      catch (Exception e) {
        log.error(e.getMessage(), e);
      }
    }

    SecureRandom oRandom = new SecureRandom();
    int node = oRandom.nextInt();

    return hexFormat(timeLow, 8) + midValue + hexFormat(node, 8);
  }

  private static int getInt(byte[] bytes)
  {
    int i = 0;
    int j = 24;

    for (int k = 0; j >= 0; k++) {
      int l = bytes[k] & 0xFF;
      i += (l << j);
      j -= 8;
    }

    return i;
  }

  private static String hexFormat(int i, int j)
  {
    String s = Integer.toHexString(i);

    return padHex(s, j) + s;
  }

  private static String padHex(String s, int i)
  {
    StringBuffer tmpBuffer = new StringBuffer();

    if (s.length() < i) {
      for (int j = 0; j < i - s.length(); j++) {
        tmpBuffer.append('0');
      }
    }

    return tmpBuffer.toString();
  }
}
